<?php
namespace Admin\Controller;
use Think\Controller;
/**
* 管理员
*/
class UserController extends Controller{
	public function login(){
		if (IS_POST) {
			$userinfo = D('user')->where("username='".I('post.username')."'")->find();
			if ($userinfo) {
				$username = $userinfo['username'];
				$password = $userinfo['password'];
				if($username == I('post.username') && $password == I('post.password')){
					cookie('adminuser', $username);
					$this->success('登录成功','/graduation/admin/index/index');exit;
				}else{
					$this->error('用户名或密码错误','/graduation/admin/user/login',1);
				}
			}else{
				$this->error('用户名不存在','/graduation/admin/user/login',1);
			}
		}
		$this->display();
	}
	public function logout(){
		cookie('adminuser', NULL);
		$this->success('安全退出','/graduation/admin/user/login');
	}
	public function editpwd(){
        if ($_COOKIE['adminuser'] == NULL) {
            $this->error('请登录','/graduation/admin/user/login',1);
        }
        if (IS_POST) {
        	$oldpwd = I('post.password');
        	$newpwd = I('post.newpassword');
        	$username = $_COOKIE['username'];
        	$userinfo = D('user')->where("username='".$username."'")->find();
        	if ($userinfo['password'] == $oldpwd) {
        		if(D('user')->where("username='".$username."'")->setField('password',$newpwd)){
        			cookie('username', NULL);
        			$this->success('修改成功, 请重新登录','/graduation/admin/user/login');exit;
        		}else{
        			$this->error('修改失败','',1);exit;
        		}
        	}else{
        		$this->error('原密码输入错误','',1);exit;
        	}
        }
		$this->display();
	}

	//反馈信息列表
	public function feedback(){
		if ($_COOKIE['adminuser'] == NULL) {
            $this->error('请登录','/graduation/admin/user/login',1);
        }
		$this->assign('backlist',D('feedback')->order('addtime desc')->select());
		$this->display();
	}

	//反馈信息内容
	public function backinfo($id){
		if ($_COOKIE['adminuser'] == NULL) {
            $this->error('请登录','/graduation/admin/user/login',1);
        }
		$this->assign('backinfo',D('feedback')->find($id));
		$this->display();
	}

	//反馈信息删除
	public function backdel($id){
		if ($_COOKIE['adminuser'] == NULL) {
            $this->error('请登录','/graduation/admin/user/login',1);
        }
		if ($feedback = D('feedback')->delete($id)) {
			$this->redirect('user/feedback');
		}
	}
}